'use strict';

const Controller = require('egg').Controller;

class IndexController extends Controller {

    /**
     * 首页
     */
    async index() {
        //todo:操作顺序为：创建 makeModel -> makeServiceOrController(0) -> makeServiceOrController(1)
        // const result =await this.ctx.service.needs.makeModel();
        // const result = await this.ctx.service.needs.makeServiceOrController(1);
        // console.log(result)

        this.ctx.body = "欢迎使用youdo服务端系统";
        // this.ctx.body = this.ctx.__('getDataSuccess')
    };

    /**
     * 创建token
     */
    async getToken() {
        let id = this.ctx.helper.getGuid16();
        const token = await this.ctx.service.units.createToken({
            id
        });
        this.ctx.status = 200;
        this.ctx.body = {
            id,
            token
        };
    };

    /**
     * 创建Diffie-Hellman算法对应的secretkey
     */
    async getDHKey() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                ctx.status = 200;
                let {
                    generateKey,
                    prime,
                    generator
                } = ctx.service.units.createDiffieHellmanForClient();
                ctx.body = {
                    gk: generateKey,
                    p: prime,
                    g: generator
                }
            } else {
                ctx.status = 200;
                ctx.body = {
                    code: 2,
                    msg: ctx.__('siginError')
                };
            }
        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    };

    /**
     * 获取地区信息数据
     */
    async getAreaInfo() {
        const {
            app,
            ctx
        } = this;
        let userid = ctx.get('uid'); //获取登录者id
        try {
            if (ctx.helper.verifyToken(ctx, userid)) {
                let levelType = ctx.query["type"]; //获取数据类型数据 0=>省份 1=>城市 2=>县级市 3=>城镇数据
                let pcode = ctx.query["pcode"]; //筛选对应数据 PCode对应值数据
                if (levelType !== undefined && levelType !== null) {
                    let returnData = [];
                    switch (parseInt(levelType)) {
                        case 0:
                            const provinceData = require('../libs/cityData/province.js');
                            returnData = provinceData;
                            if (pcode !== undefined && pcode !== null) {
                                returnData = provinceData.filter(x => {
                                    return x.PCode === pcode
                                });
                            }
                            break;
                        case 1:
                            const cityData = require('../libs/cityData/city.js');
                            returnData = cityData;
                            if (pcode !== undefined && pcode !== null) {
                                returnData = cityData.filter(x => {
                                    return x.PCode === pcode
                                });
                            }
                            break;
                        case 2:
                            const areaData = require('../libs/cityData/area.js');
                            returnData = areaData;
                            if (pcode !== undefined && pcode !== null) {
                                returnData = areaData.filter(x => {
                                    return x.PCode === pcode
                                });
                            }
                            break;
                        case 3:
                            const streetsData = require('../libs/cityData/streets.js');
                            returnData = streetsData;
                            if (pcode !== undefined && pcode !== null) {
                                returnData = streetsData.filter(x => {
                                    return x.PCode === pcode
                                });
                            }
                            break;
                    }
                    ctx.status = 200;
                    ctx.body = {
                        code: 1,
                        msg: ctx.__('getDataSuccess'),
                        data: returnData
                    };
                } else {
                    ctx.status = 200;
                    ctx.body = {
                        code: 0,
                        msg: ctx.__('argsError')
                    };
                }

            }

        } catch (error) {
            ctx.status = 500;
            ctx.body = {
                code: -1,
                msg: error.message
            };
        }
    }

    /**
     * 获取AEP服务器时间戳
     * @returns {*} token
     */
    async getTimestamp() {
        const {app, ctx} = this;
        const result = await ctx.curl(`https://ag-api.ctwing.cn/echo`, {
            method: 'GET',
            contentType: 'application/x-www-form-urlencoded',
            rejectUnauthorized: false,
        });
        let returnData = {
            status: 'error',
            message: '获取数据失败'
        }
        if (result.status === 200) {
            let res = result.res;
            returnData = {
                status: 'success',
                message: '获取成功',
                data: res.headers.timestamp
            }
        }
        ctx.status = 200;
        ctx.body = returnData;
    }

}

module.exports = IndexController;
